newscat = {}

// - Start
newscat.listCat;//Khai báo var
//Print category Option for dropdown box
newscat.level = 0;
newscat.printCat = function(parentId){
    $.each(newscat.listCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            newscat.level ++;
            var tmp = "";
            var i = newscat.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            $(' #parentId').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            newscat.printCat(cat.id);
            newscat.level --;
        }
    });
}
//Print List NewsCat
newscat.printCatList = function(parentId){
    $.each(newscat.listCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            newscat.level ++;
            var tmp = "";
            var i = newscat.level;
            while(i){
                tmp += "- - ";  
                i--;
            } 
            
            if(cat.published == 1)
                cat.published = '<a onclick="newscat.changeStatus('+ cat.id +','+ newscat.level +')"><img src="'+ fly.assetsUrl +'/images/icon-enable.png" /></a>';
            else
                cat.published = '<a onclick="newscat.changeStatus('+ cat.id +','+ newscat.level +')"><img src="'+ fly.assetsUrl +'/images/icon-disable.png" /></a>';
                
            $('table').append('<tr level="'+newscat.level+'" align="center" for="'+cat.id+'">\
                <td><input type="checkbox" for="'+cat.id+'"/></td>\
                <td><p>'+cat.id+'</p></td>\
                <td style="padding-left:50px;text-align:left"><p>'+tmp+cat.name+'</p></td>\
                <td><p>'+cat.description+'</p></td>\
                <td><p>'+cat.order+'</p></td>\
                <td><p>'+cat.published+'</p></td>\
                <td><p><a onclick="newscat.edit('+cat.id+')">Sửa</a></p></td>\
                <td><p><a onclick="newscat.del('+cat.id+')">Xóa</a></p></td>\
            </tr>');
            newscat.printCatList(cat.id);
            newscat.level --;
        }
    })
}
// - End

newscat.list = function(){
    main.init();
    fly.ajax({   
        service:'/newsCategory/list',
        success: function(result){
            if(result.status){
                newscat.listCat = result.data;
                $('#content').html(fly.template('http://naima.vn/newscat/list.html', {
                    //count_data: result.data.length
                    }));   
                
                $('#list-checkall').click(function(){
                    $('input[type=checkbox][for]').attr('checked', $('#list-checkall').is(':checked'))
                });
                $('#total-count').text(result.data.length);
                newscat.printCatList(0);
                newscat.printCat(0);
            }
            else{
                popup.msg(result.message);
            }
        }
    });
}
//Create new news category
newscat.create = function(){
    popup.open('popup-newscat-form', 'Thêm menu tin tức mới', fly.template('http://naima.vn/newscat/form.html', {
        data:null
    }), [

    {
        title:'Thêm mới', 
        fn:function(){            
            fly.submit({
                id:'newscat-form',
                service: '/newsCategory/create',
                success: function(result){               
                    if(result.status){
                        popup.msg(result.message);
                        popup.close('popup-newscat-form');
                        $('table tr').first().after('');
                        fly.ajax({
                            service:'/newsCategory/list',
                            success: function(rs){
                                if(rs.status){
                                    newscat.listCat = rs.data;
                                    $('#content').html(fly.template('http://naima.vn/newscat/list.html', {
                                        data:rs.data
                                    }));                                
                                    newscat.printCatList(0);
                                }
                                else{
                                    popup.msg(rs.message);
                                }
                            }
                        });
                    }
                    else
                        popup.msg(result.message);
                }
            });
        }
    },
    {
        title:'Hủy bỏ',
        fn:function(){
            popup.close('popup-newscat-form');
        }
    }
    ]);
    fly.ajax({
        service:'/newsCategory/list',
        success: function(result){
            newscat.listCat = result.data;
            newscat.printCat(0);
        }
    });
    //fly.editor('newscat-detail',{width:500});  
}

newscat.edit = function(id){
    fly.ajax({
        service: '/newsCategory/get',
        loading: false,
        data: {
            id: id
        },
        success: function(data){
            if(!data.status)
                popup.msg(data.message);
            else{
                popup.open('popup-editcat-form', 'Chỉnh sửa danh mục tin tức', fly.template('http://naima.vn/newscat/form.html', {
                    data:data.data
                }), [

                {
                    title:'Chỉnh sửa', 
                    fn:function(){            
                        fly.submit({
                            id:'newscat-form',
                            service: '/newsCategory/edit/'+id,
                            success: function(result){
                                if(result.status){
                                    popup.msg(result.message);
                                    popup.close('popup-editcat-form');
                                    $('table tr').first().after('');
                                    fly.ajax({
                                        service:'/newsCategory/list',
                                        success: function(rs){
                                            if(rs.status){
                                                newscat.listCat = rs.data;
                                                $('#content').html(fly.template('http://naima.vn/newscat/list.html', {
                                                    data:null
                                                }));                                
                                                newscat.printCatList(0);
                                            }
                                            else{
                                                popup.msg(rs.message);
                                            }
                                        }
                                    });
                                }
                                else
                                    popup.msg(result.message);
                            }
                        });
                    }
                },
                {
                    title:'Hủy bỏ',
                    fn:function(){
                        popup.close('popup-editcat-form');
                    }
                }
                ]);
                fly.ajax({
                    service:'/newsCategory/list',
                    loading: false,
                    success: function(res){
                        newscat.listCat = res.data;
                        newscat.printCat(0);
                        $('#parentId').val(data.data.parentId); 
                    }
                });                         
                fly.editor('newscat-detail',{width:500});  
            }
        }
    });    
}

newscat.del = function(id){
    popup.confirm("Bạn có chắc chắn muốn xóa danh mục này?", function(){
        fly.ajax({
            service: '/newsCategory/delete/',
            data: {
                id: id
            },
            success: function(result){
                if(result.status){
                    popup.msg(result.message);
                    $('.table tr[for='+id+']').remove();
                    $.each(result.data, function(){
                        var tmp = this;
                        $('.table tr[for='+tmp.id+']').remove();
                    });
                }else{
                    popup.msg(result.message);
                }
            }
        });
    });
}

newscat.checkAll = function(){
    if($('#list-checkall').is(":checked"))
        $('input[type="checkbox"]').attr('checked','checked');    
    else
        $('input[type="checkbox"]').removeAttr('checked');
}
newscat.delAll = function(){
    var data = Array();
    var i = 0;
    $('input[type=checkbox][for]').each(function(){
        if($(this).is(':checked')){
            data[i] = parseInt($(this).attr('for'));
            i++;
        }
    });
    if(data.length <= 0){
        popup.msg("Bạn chưa chọn menu nào.");
        return;
    }
    popup.confirm("Bạn có chắc chắn muốn xóa danh mục đã chọn?", function(){
        fly.ajax({
            service: '/newsCategory/deleteall',
            data:{
                ids:JSON.stringify(data)
            },
            success: function(result){
                if(result.status){
                    popup.msg(result.message);
                    for(var i=0; i<result.data.length; i++){
                        $('.table tr[for='+result.data[i]+']').remove();
                    }
                }
                else{
                    popup.msg(result.message);    
                }
            }
        });
    });
}

newscat.changeStatus = function(id,level){
    fly.ajax({
        service: '/newsCategory/changeStatus',
        data: {
            id:id,       
        },
        success: function(result){    
            var tmp = "";
            var i = level;
            while(i){
                tmp += "- - ";
                i--;
            }  
            $('.table tr[for='+result.data.id+']').replaceWith(fly.template('http://naima.vn/newscat/row.html',{
                data:result.data, 
                lvl: level,
                tmp: tmp,
            }));
        }
    });

}